{% extends theme("base.html") %}

{% import "snippets.html" as snippets with context %}
{% import "utils.html" as utils with context %}

{% set logo = snippets.logo() %}
{% set before_header = snippets.before_header() %}
{% set after_header = snippets.after_header() %}
{% set before_footer = snippets.before_footer() %}
{% set after_footer = snippets.after_footer() %}
{% set license_info = snippets.license_info() %}
{% set credits = snippets.credits() %}
{% set creditlogos = snippets.creditlogos() %}
{% set user_actions, item_navigation, item_actions = theme_supp.get_local_panel(fqname) %}
{% set current_url = request.url %}
{% set current_path = request.path %}
{% set current_url_showview = url_for_item(endpoint='frontend.show_item', item_name=item_name) %}
{% set login_url = theme_supp.login_url() %}

{# Helper macro to generate the local panel #}
{% macro local_panel_main(panelname) -%}
    {% for endpoint, href, iconcls, label, title, exists in panelname %}
        {# iconcls should be defined, defined for all local_panel_items, see themes/__init.py #}
        {{ hyperlink(href, title, exists, label, iconcls, "list-group-item wikilink") }}
    {% endfor %}
{% endmacro %}

{# Helper macro to generate links #}
{% macro hyperlink(href, title, exists, label, iconcls, mainclass) -%}
    <a href="{{ href }}"
        {% if not exists %} class="moin-nonexistent {% if mainclass %}{{ mainclass }} {% endif %}"
        {% else %}
            {% if mainclass %}
                class="{{ mainclass }}{% if current_url == href %} active{% endif %}"
            {% elif current_url == href %}
                class="active"
            {% elif current_path == href %}
                class="active"
            {% endif %}
        {% endif %}
        rel="nofollow"
        {% if title %} title="{{ title }}" {% endif %}>
        {% if iconcls %} <i class="{{ iconcls }}"> </i> {% endif %}
        {% if href == login_url %}
            {# show "login" for all screen sizes #}
            {{ label }}
        {% else %}
            {# hide username, settings, logout labels for sm, xs screen sizes, only icons will be visible #}
            <span class="hidden-sm hidden-xs">{{ label }}</span>
        {% endif %}
    </a>
{% endmacro %}

{# Helper macro to generate a search form #}
{% macro make_search_form(div_class, form_class) %}
    <div class="{{ div_class }}">
        <form class="{{ form_class }}" action="{{ url_for('frontend.search') }}" method="get" role="search">
            <div class="input-group">
                <input name='q' type="text" class="form-control" placeholder="Search ...">
                <div class="input-group-btn">
                    <button class="btn btn-primary" type="submit">
                        <i class="fa fa-search"></i>
                    </button>
                </div>
            </div>
        </form>
    </div>
{% endmacro %}

{% block layout %}
    <div class="container-fluid">
        <div id="moin-main-wrapper" class="row">

            <header class="col-md-2 col-sm-2 col-xs-4 moin-sidebar">
                <div class="moin-logo">
                    {% if logo %}
                        <a href="{{ url_for('frontend.show_item', item_name=cfg.item_root) }}">
                            {{ logo }}
                        </a>
                    {% endif %}
                </div>
                <div class="moin-sidebar-panels">
                    {% set navibar_items = theme_supp.navibar(fqname) %}
                    {% set quicklinks = 'false' %}

                    {% if search_form %}
                        {{ make_search_form("hidden-lg hidden-md hidden-sm", "moin-sidebar-form") }}
                    {% endif %}

                    {% if navibar_items %}
                        <div class="moin-list-heading">
                            <div class="list-group-item-heading">{{ _("Site Navigation") }}</div>
                            {% for cls, url, text, title in navibar_items if cls != 'userlink' %}
                                <a class="list-group-item {{ cls }}
                                    {% if url in [current_url, current_path, current_url_showview] %} active{% endif %}" href="{{ url }}"
                                    {% if title %}title="{{ title }}"{% endif %}>
                                    {{ text|shorten_item_name }}
                                </a>
                            {% endfor %}
                        </div>
                    {% endif %}

                    {% for cls, url, text, title in navibar_items if cls == 'userlink' %}
                        {% if loop.first %}<div class="moin-list-heading">
                            <div class="list-group-item-heading">{{ _("Quick Links") }}</div>
                        {% endif %}
                        <div class="moin-quicklink">
                            <a class="moin-quicklink-label {{ cls }}" href="{{ url }}" {% if title %}title="{{ title }} : {{ url|safe }}"{% else %}title="{{ url|safe }}"{% endif %}>{{ text|shorten_item_name(20) }}
                            </a>
                        </div>
                        {% if loop.last %}</div>{% endif %}
                    {% endfor %}

                    {% if self.local_panel %}
                        {{ self.local_panel() }}
                    {% endif %}
                </div> {# moin-sidebar-panels #}
            </header> {# moin-sidebar #}

            <div class="col-md-10 col-sm-9 col-xs-12">
                {{ before_header }}
                <nav class="navbar moin-navbar-inverse">
                    <div class="row">
                        <div class="col-md-3 col-sm-5 col-xs-8">
                            <div class="moin-navbar-header">

                                <button type="button" id="hideshowsidebar" class="navbar-toggle">
                                    <span class="icon-bar"></span>
                                    <span class="icon-bar"></span>
                                    <span class="icon-bar"></span>
                                </button>

                                {% if cfg.sitename %}
                                    <a class="navbar-brand" href="{{ url_for('frontend.show_item', item_name=cfg.item_root) }}">
                                        {{ cfg.sitename }}
                                    </a>
                                {% endif %}

                            </div>
                        </div>

                        <div class="col-md-6 col-sm-4 col-xs-4 moin-loginsettings">
                            <div class="moin-navbar-user">
                                <ul class="nav navbar-nav moin-pull-right">
                                    {% if user.valid -%}
                                        {% set avatar = user.avatar(20) %}
                                        {% if avatar %}
                                            <li><img id="moin-avatar" src="{{ avatar }}" /></li>
                                        {%- endif %}
                                        {% if user.name -%}
                                            {% set wiki_href, aliasname, title, exists = theme_supp.userhome() %}
                                            <li>
                                                {{ hyperlink(wiki_href, title, exists, aliasname, "fa fa-user fa-inverse", None) }}
                                            </li>
                                            {% if 'frontend.usersettings' not in cfg.endpoints_excluded -%}
                                                <li title="{{ _('Settings') }}">
                                                    {{ hyperlink(url_for('frontend.usersettings'), None, True,
                                                        _('Settings'), "fa fa-cog fa-inverse", None) }}
                                                </li>
                                            {%- endif %}
                                        {%- endif %}
                                        {% if user.auth_method in cfg.auth_can_logout %}
                                            <li title="{{ _('Logout') }}">
                                                {{ hyperlink(url_for('frontend.logout', logout_submit=1), None, True,
                                                        _('Logout'), "fa fa-sign-out fa-inverse", None) }}
                                            </li>
                                        {% endif %}
                                    {% else %}

                                        {% if login_url %}
                                            <li>
                                                {{ hyperlink(login_url, None, True, _('Login'), None, None) }}
                                            </li>
                                        {% endif %}
                                    {%- endif %}
                                </ul>
                            </div>
                        </div>
                        {% if search_form %}
                            {{ make_search_form("col-sm-3 hidden-xs", "moin-navbar-form") }}
                        {% endif %}
                    </div>
                </nav> {# navbar moin-navbar-inverse #}

                {{ after_header }}
                {{ utils.breadcrumbs(theme_supp.path_breadcrumbs, title_name) }}

                <div class="moin-content" role="main">
                    {# id -> moin-flash is used to add special alerts for ex. for changes to User Settings via JS, see common.js #}
                    <div id="moin-flash">
                        {% for category, msg in get_flashed_messages(with_categories=true) %}
                            <div class="moin-flash moin-flash-{{ category }}">
                                {{ msg }}
                                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                            </div>
                        {% endfor %}
                    </div>

                    {% block views %}
                    {% endblock %}

                    {% set content_id = self.local_panel and 'moin-content' or 'moin-content-no-panel' %}
                    <div id="{{ content_id }}" lang="{{ theme_supp.content_lang }}" dir="{{ theme_supp.content_dir }}">
                        {% block content %}
                            {# All content templates should implement this block. #}
                        {% endblock %}
                    </div>
                </div>

                {{ before_footer }}

                {% block footer %}
                    <footer class="navbar moin-footer">
                        {% block footer_hr %}<hr/>{% endblock %}
                        {% block footer_meta %}{% endblock %}
                        {{ creditlogos }}
                        {{ credits }}
                        {{ license_info }}
                    </footer>
                {% endblock %}

                {{ after_footer }}

            </div> {# parent of header, breadcrumbs, content, footer #}
        </div>

    </div>
{% endblock %}

{% block body_scripts %}
    {{ super() }}
    <script src="{{ url_for('serve.files', name='bootstrap', filename='js/bootstrap.min.js') }}"></script>
    <script src="{{ url_for('static', filename='js/basic.js') }}"></script>
{% endblock %}
